package edu.xust.MetadataGenerator.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class JXLS106Helper {
	private final static Logger logger = LogManager.getLogger(JXLS106Helper.class);
	public static String TEMP = "template.xlsx";
	private static JXLS106Helper _helper = null;
	
	private JXLS106Helper(){
		
	}
	
	public static JXLS106Helper getInstance(){
		if(null == _helper){
			_helper = new JXLS106Helper();
		}
		return _helper;
	}
	
	public boolean generateExcel(Map beans, File to){
		logger.debug(to.getAbsolutePath());
		boolean result = false;
		String from = TEMP;
		if(loadTemplate(from)){
			mkDir(to.getAbsoluteFile().getParentFile());
			File fromFile = new File(from);
			result = _generateExcel(fromFile.getAbsolutePath(), beans, to.getAbsolutePath());
			fromFile.delete();
		}
		return result;
	}
	
	private boolean _generateExcel(String from, Map beans, String to) {
		/*
		logger.debug(from);
		boolean result = true;
		XLSTransformer transformer = new XLSTransformer();
		try {
			transformer.transformXLS(from, beans, to);
		} catch (ParsePropertyException e) {
			logger.debug(e.getMessage());
			result = false;
		} catch (IOException e) {
			logger.debug(e.getMessage());
			result = false;
		} catch (InvalidFormatException e) {
			logger.debug(e.getMessage());
			result = false;
		}

		return result;
		*/
		return true;
	}
	
	public String getCurrentDir(){
		File dir = new File(".");
		return dir.getAbsolutePath();
	}
	
	/*
	 * Load jxls template file from resource.
	 */
	public boolean loadTemplate(String dest){
		boolean result = false;
		try {
			InputStream is = this.getClass().getResourceAsStream("/jxls.xlsx");
				if(null != is){
				File desFile = new File(dest);
				mkDir(desFile.getAbsoluteFile().getParentFile());
				desFile.createNewFile();
				FileOutputStream fos = new FileOutputStream(desFile);
				int bytesRead;
				byte[] buf = new byte[4 * 1024]; // 4K buffer
				while ((bytesRead = is.read(buf)) != -1) {
					fos.write(buf, 0, bytesRead);
				}
				fos.flush();
				fos.close();
				is.close();
				result = true;
			}else{
				logger.debug("Fail to read template!");
			}
		} catch (IOException e) {
			logger.debug(e.getMessage());
		}
		return result;
	}
	
	public boolean mkDir(File file) {
		file = file.getAbsoluteFile();
		boolean result = false;
		if(null != file){
			logger.debug(file.getAbsolutePath());
			File parent = file.getParentFile();
			if(null != parent){
				if (parent.exists()) {
					file.mkdir();
					result = true;
					logger.debug("create " + file.getAbsolutePath());
				} else {
					if(mkDir(parent)){
						file.mkdir();
						result = true;
						logger.debug("create " + file.getAbsolutePath());
					}
				}
			}else{
				logger.debug(file.getAbsolutePath() + ":" + file.getParent());
			}
		}
		return result;
	}
}
